﻿using UnityEngine;
using System.Collections;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

//控制游戏物品到达目标位置
public class MySeek : Action {

	public SharedTransform target;//目标位置
	public float speed = 6.0f;//移动速度
	public float arriveDistance = 0.1f;//碰撞距离
	private float sqrArriveDis;//碰撞距离平方

	public override void OnStart ()
	{
		sqrArriveDis = arriveDistance * arriveDistance;
	}

	public override TaskStatus OnUpdate ()
	{
		if (target == null || target.Value==null) {
			return TaskStatus.Failure;
		}

		//设定朝向
		transform.LookAt (target.Value.position);

		//向目标位置以速度speed移动，并必须把移动结果赋值给transform.position
		transform.position = Vector3.MoveTowards (transform.position, target.Value.position, speed * Time.deltaTime);

		//计算当前位置与目标位置的差值( sqrMagnitude性能优于Magnitude)
		float dis = (transform.position - target.Value.position).sqrMagnitude;

		//如果距离<设定的碰撞距离，则表示到达目标，返回Success；否二继续running
		if (dis < sqrArriveDis) {
			return TaskStatus.Success;
		}

		return TaskStatus.Running;
	}
}
